package nxu.contral;


import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Kv;
import nxu.entity.User;
import nxu.filter;
import nxu.service.UserService;

import java.time.LocalDateTime;
import java.time.format.TextStyle;
import java.util.*;

@Path(value = "/",viewPath = "/")
public class MainContral extends Controller {


    private static UserService userService= new UserService();
    public  void hello(){



     renderText("你好！");
     renderQrCode("hi!hi!hi!哈哈哈哈哈",240,240);
    }
    public  void bye(){

    renderText("再见！");
    renderQrCode("再次相见",240,240);
    }

    public  void json(){

        String jsonStr="{\"code\":200,\"message\":\"ok\",\"data\":\"hhh\"}";
        Map<String,Object> jsonData = new HashMap<>();
        jsonData.put("code",200);
        jsonData.put("message","hhhh");
        jsonData.put("data","hello"+get("username"));
        renderJson(jsonData);

    }
    public void today (){

        LocalDateTime now = LocalDateTime.now();

        Kv jsonData = Kv.create()
                .set("year",now.getYear())
                .set("month",now.getMonth())
                .set("day",now.getDayOfMonth())
                .set("hour",now.getHour())
                .set("minute",now.getMinute())
                .set("weekday",now.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA));
        renderJson(jsonData);
    }


    public  void  hi(){
        String username = get("username");
        Integer age = getInt("age");
        set("username",username);
        set("age",age);
        renderFreeMarker("hello.ftl");

    }



    public void doCheck(){
        String username = get("username");
        String password = get("password");
        boolean success=userService.checkLogin(username,password);
        if(success){
            renderHtml("success");
        }
        else {
            renderHtml("error");
        }


    }
    @Before(filter.class)
    public void success(){
        String username = getCookie("username");
        renderHtml("hello  "+username);
    }
    @Before(filter.class)
    public void main(){
            renderHtml("又来了呀老弟");


    }

    public void login(){
        renderFreeMarker("login.ftl");

    }
    public  void  register(){
        List<String>homeTownList= List.of("宁夏","北京","辽宁");
        set("homeTownList",homeTownList);

        renderFreeMarker("register.ftl");
//123
    }


    public  void  doRegister(){
        User user = getBean(User.class,"",true);
//        String uname= get("username");
//        String hometown= get("hometown");
//        String uid= get("uid");
//        String password= get("password");

        System.out.println(user);
//        renderHtml("注册成功");
//        renderHtml("欢迎来自"+hometown+"的"+uname);
        boolean success= userService.register(user);
        String message = success? "success":"error";

        renderHtml(message);
    }





}
